const validate = RxFormValidator.validator({
    rxElemValidators: [
        RxElemValidator.notBlank('content', '[内容] 不能为空'),
        RxElemValidator.notBlank('category', '[分类] 不能为空'),
        RxElemValidator.notBlank('source', '[记录来源] 不能为空'),
        RxElemValidator.notBlank('status', '[活跃状态] 不能为空'),

        RxElemValidator.limitLen('content', '[黑白名单] 长度最大为 255 位', 255),
        RxElemValidator.limitLen('comments', '[备注] 长度最大为 255 位', 255),
    ],
})

class SysBlackWhiteList {
    constructor({
                    id = '',
                    content = '',
                    status = '',
                    priority = '',
                    category = '',
                    source = '0',
                    createTime = '',
                    comments = '',
                } = {}) {
        this.id = id
        this.content = content
        this.status = status
        this.priority = priority
        this.category = category
        this.source = source
        this.createTime = createTime
        this.comments = comments
    }
}

const app = new BasicTableVue({
    data() {
        return {
            api: sysBlackWhiteListApi,
            formData: new SysBlackWhiteList(),
            dialogShow: false,
            dtitle: '',
            formLabelWidth: '70px',
            columns: [
                new TableColumn({width: 30, type: 'selection'}),
                new TableColumn({prop: 'url', title: '链接', customComponent: true}),
                new TableColumn({prop: 'content', title: '内容'}),
                TableColumn.generate({prop: 'priority', title: '优先级', formatter: this.valFormat}),
                new TableColumn({
                    prop: 'category', title: '分类',
                    formatter: fieldFormatter.facade(fieldFormatter.sysWhitelist.categoryMap),
                }),
                new TableColumn({prop: 'source', title: '记录来源', customComponent: true}),
                new TableColumn({
                    prop: 'status', title: '活跃状态',
                    formatter: fieldFormatter.facade(fieldFormatter.sysWhitelist.statusMap),
                }),
                TableColumn.generate({
                    prop: 'createTime', title: '创建时间',
                    formatter: TableColumn.datetimeFormat,
                }),
                new TableColumn({prop: 'comments', title: '备注'}),
                new TableColumn({prop: 'operating', title: '操作', customComponent: true}),
            ],
        }
    },
    methods: {
        createForm() {
            return new SysBlackWhiteListVo()
        },
        urlContent(val) {
            if (val.indexOf('http') === -1) {
                return 'http://' + val
            } else {
                return val
            }

        },
        goUrl(row) {
            debugger
            var content = row.content
            var source = row.source
            var url
            if (source != 0) {
                url = fieldFormatter.generator(fieldFormatter.sysWhitelist.pagePathMap)(source)
                forward(url, {
                    content: content,
                    isFromWhitelist: true,
                })
            }
            /*else{
                url = "/system-config/admin_whitelist"
            }*/



        },
        changeCreateTime(timeArray) {
            [start, end] = timeArray || []
            this.form.createTimeStart = start
            this.form.createTimeEnd = end
        },
        async downloadTemplate() {
            await sysBlackWhiteListApi.downloadTemplate('黑白名单导入模板.xlsx')
        },
        addInfo() {
            this.dtitle = '新增'
            this.dialogShow = true
            this.formData = new SysBlackWhiteList()
        },
        updateInfo(row) {
            this.dtitle = '修改'
            var cloneDeep = _.cloneDeep(row)
            cloneDeep.priority = parseInt(cloneDeep.priority, 2)
            this.formData = cloneDeep
            this.dialogShow = true
        },
        valFormat(row, column, val, key) {
            return parseInt(val, 2)
        },
        async updateOrAddData() {
            const formData = this.formData

            if (!(await await this.$validator.validateAll())) {
                rxPrompt.msg("输入存在错误");
                return;
            }

            var res;
            if (formData.id) {
                //修改
                res = await sysBlackWhiteListApi.update(formData)
            } else {
                //新增
                res = await sysBlackWhiteListApi.add(formData)
            }
            const {msg = '', code = 0} = res;
            if (code === 200) {
                await Promise.all([
                    rxPrompt.successMsg(this.dtitle + '成功'),
                    this.searchPage(),
                ])
                this.dialogShow = false
            } else {
                await rxPrompt.dangerMsg(this.dtitle + `失败,${msg}`);
            }
        },
        async del({id, editable}) {
            if (!id || editable === 0) {
                await rxPrompt.msg('操作失败')
                return
            }
            const res = await rxPrompt.confirm('你确认要删除该记录吗?？', ['确认', '取消'])
            if (!res) {
                return
            }
            const result = await this.api.delete(id)
            if (!result) {
                return
            }
            await Promise.all([
                rxPrompt.successMsg('删除成功'),
                this.searchPage(),
            ])
        },
        async deleteData(ids) {
            undelIds = this.selectedList.filter(obj => parseInt(obj.editable) === 1 && parseInt(obj.source) !==0).map(obj => obj.id)
            if(undelIds.length !== 0){
                await rxPrompt.msg('只能删除来源为 其他 的数据')
                return
            }
            ids = this.selectedList.filter(obj => parseInt(obj.editable) === 1 && parseInt(obj.source) ===0).map(obj => obj.id)
            return sysBlackWhiteListApi.deleteBatch(ids)
        },
        async init() {

        },

    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },

})
